package org.activequant.util.store;

import java.io.Serializable;

import org.activequant.store.IStoreService;

/**
 * Store proxy that puts all objects into a "namespace" by adding a prefix
 * to the object's name. Useful to share one store between 
 * different objects (each object gets its own proxy with different namespace).
 * <p>
 * NamespaceStoreProxy implements IStoreService.<br/>
 * Holds the following associated variables:
 * <ul>
 * <li>store(IStoreService)</li>
 * <li>namespace(String)</li>
 * </ul>
 * <b>History:</b><br>
 *  - [03.01.2008] Created (Mike Kroutikov)<br>
 *
 *  @author Mike Kroutikov
 */
public class NamespaceStoreProxy implements IStoreService {
	/**
	 * private final IStoreService store;
	 */
	private final IStoreService store;
	/**
	 * private final String namespace;
	 */
	private final String namespace;
	/**
	 * constructs a NamespaceStoreProxy(implements IStoreService) using the given store(IStoreService) and namespace(String)
	 * to set its associated store(IStoreService) and namespace(String).
	 * @param store
	 * @param namespace
	 */
	public NamespaceStoreProxy(IStoreService store, String namespace) {
		this.store = store;
		this.namespace = namespace;
	}
	/**
	 * returns a Serializable by invoking <code>store.load(${namespace} + ${name})</code> on the associated store(IStoreService) using the given name(String)
	 * and associated namespace(String)
	 */
	public Serializable load(String name) throws Exception {
		return store.load(namespace + name);
	}
	/**
	 * saves the given object(Serializable) by invoking <code>store.save(${namespace} + ${name},${object})</code> using the given name(String)
	 * and associated namespace(String)
	 */
	public void save(String name, Serializable object) throws Exception {
		store.save(namespace + name, object);
	}
}
